package com.jdk.demo.asynchronousFileChannel.获取独占锁;

import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.FileLock;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Future;

// 获取此文件的独占锁
public class Test2 {

	public static void main(String[] args) throws Exception {
		Path path = Paths.get("E:/zsl/temp2.txt");
		AsynchronousFileChannel channel =  AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
		System.out.println("B lock begin=" + System.currentTimeMillis());
		Future<FileLock> future = channel.lock();
		System.out.println("B lock end=" + System.currentTimeMillis());
		FileLock lock = future.get();
		System.out.println("B get lock time=" + System.currentTimeMillis());
		lock.release();
		channel.close();
	}

}
